//
//  CoobjcViewController.swift
//  DemoProject
//
//  Created by Hao on 2019/3/11.
//  Copyright © 2019 hao. All rights reserved.
//

import UIKit
import coobjc
import coswift

fileprivate class PromiseModel {
    
    func loadChannels() -> Promise<String> {
        let promise = Promise<String>()
        DouBanProvider.request(TDouBanAPI.channels) { result in
            switch result {
            case let .success(respone):
                if let data = String(data: respone.data, encoding: String.Encoding.utf8) {
//                    print(info: data)
                    promise.fulfill(value: data)
                }
            case let .failure(error):
//                print(info: error)
                promise.reject(error: error)
            }
        }
        return promise
    }
    
}

class CoobjcViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        co_launch {
            let result = try await(closure: {
                PromiseModel.init().loadChannels()
            })
            switch result {
            case .fulfilled(let val):
                // the promise fulfilled
                print(info: val)
                break
            case .rejected(let error):
                // the promise reject
                print(info: error)
                break
            }
        }
    }

}
